<script setup>
import { User, Lock } from '@element-plus/icons-vue'
import { ref } from 'vue'
import useUserStore from '@/store/modules/user'
import { useRouter } from 'vue-router'
import { ElNotification } from 'element-plus'
import { getTime } from '@/utils/time'

const userStore = useUserStore()
const router = useRouter()
//按钮加载控制
const loading = ref(false)
const loginForm = ref({
  username: 'admin',
  password: '111111',
})
//点击登录
const handelLogin = async () => {
  //保证校验通过再发请求
  await loginRef.value.validate()
  // console.log('isPass', isPass)

  loading.value = true
  try {
    //保证成功
    await userStore.userLogin(loginForm.value)
    router.push('/')
    loading.value = false
    //提示信息
    ElNotification({
      type: 'success',
      message: '登录成功',
      title: `HI,${getTime()}好`,
    })
  } catch (error) {
    loading.value = false
    //登陆失败
    ElNotification({
      type: 'error',
      message: error.message,
    })
  }
}
const loginRef = ref()
//表单校验需要的配置对象
const rules = {
  username: [{ required: true, min: 5, max: 10, message: '账号长度至少5位', trigger: 'blur' }],
  password: [{ required: true, min: 6, max: 15, message: '密码的长度至少6位', trigger: 'blur' }],
}
</script>
<template>
  <div class="login-container">
    <el-row>
      <!-- 占位用 -->
      <el-col :span="12" :xs="0"></el-col>
      <!-- 右侧表单 -->
      <el-col :span="12" :xs="24">
        <el-form class="login-form" :model="loginForm" :rules="rules" ref="loginRef">
          <h1>Hello</h1>
          <h2>欢迎来到于晏甄选</h2>
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" :prefix-icon="User"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              type="password"
              :prefix-icon="Lock"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button :loading="loading" @click="handelLogin" class="login-btn" type="primary">
              登录
            </el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<style scoped lang="scss">
.login-container {
  width: 100%;
  height: 100vh;
  background-color: red;
  background: url('@/assets/images/background.jpg') no-repeat center;
  background-size: cover;
}
.login-form {
  position: relative;
  width: 80%;
  top: 30vh;
  background: url('@/assets/images/login_form.png');
  background-size: cover;
  padding: 40px;
  h1 {
    color: #fff;
    font-size: 40px;
  }
  h2 {
    color: #fff;
    font-size: 20px;
    margin: 20px 0;
  }
  .login-btn {
    width: 100%;
  }
}
</style>
